package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.websocket.WebSocketServer;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_NOTIFY_GROUP,
        topic = MQConstans.CREATE_ORDER,
        selectorExpression = MQConstans.CREATE_ORDER_SUCCESS_TAG + "||" + MQConstans.CREATE_ORDER_FAIL_TAG
)
public class OrderNotifyMQListener implements RocketMQListener<Map> {


    @SneakyThrows
    @Override
    public void onMessage(Map map) {
        System.out.println("通知消费者");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String uuid = (String) map.get("uuid");
        WebSocketServer webSocketServer = WebSocketServer.clients.get(uuid);
        if (webSocketServer != null) {
            webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(map));
        }
    }
}
